home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / PowerD / Dmod / dmod_MUI / examples / Class1.d < prev    next >
Encoding:
Text File  |  2002-10-28  |  5.6 KB  |  205 lines

  1. /*
  2. ** Demosource on how to use customclasses in D.
  3. ** Based on the C example 'Class1.c'.
  4. ** Translated to E by Jan Hendrik Schulz
  5. ** Translated to D by Martin <MarK> Kuchinka
  6. */
  7.  
  8. MODULE    'muimaster',
  9.             'libraries/mui',
  10.             'lib/amiga',
  11.             'intuition/classes',
  12.             'intuition/classusr',
  13.             'intuition/screens',
  14.             'intuition/intuition',
  15.             'utility/tagitem'
  16.  
  17. /***************************************************************************/
  18. /* Here is the beginning of our simple new class...                        */
  19. /***************************************************************************/
  20.  
  21. /*
  22. ** This is an example for the simplest possible MUI class. It's just some
  23. ** kind of custom image and supports only two methods: 
  24. ** MUIM_AskMinMax and MUIM_Draw.
  25. */
  26.  
  27. /*
  28. ** This is the instance data for our custom class.
  29. ** Since it's a very simple class, it contains just a dummy entry.
  30. */
  31.  
  32. OBJECT mydata
  33.     dummy:LONG
  34.  
  35.  
  36. /*
  37. ** AskMinMax method will be called before the window is opened
  38. ** and before layout takes place. We need to tell MUI the
  39. ** minimum, maximum and default size of our object.
  40. */
  41.  
  42. PROC AskMinMax(cl:PTR TO IClass,obj,msg:PTR TO MUIP_AskMinMax)
  43.  
  44.     /*
  45.     ** let our superclass first fill in what it thinks about sizes.
  46.     ** this will e.g. add the size of frame and inner spacing.
  47.     */
  48.  
  49.     DoSuperMethodA(cl,obj,msg)
  50.  
  51.     /*
  52.     ** now add the values specific to our object. note that we
  53.     ** indeed need to *add* these values, not just set them!
  54.     */
  55.  
  56.     msg.MinMaxInfo.MinWidth += 100
  57.     msg.MinMaxInfo.DefWidth += 120
  58.     msg.MinMaxInfo.MaxWidth += 500
  59.  
  60.     msg.MinMaxInfo.MinHeight += 40
  61.     msg.MinMaxInfo.DefHeight += 90
  62.     msg.MinMaxInfo.MaxHeight += 300
  63.  
  64. ENDPROC
  65.  
  66.  
  67. /*
  68. ** Draw method is called whenever MUI feels we should render
  69. ** our object. This usually happens after layout is finished
  70. ** or when we need to refresh in a simplerefresh window.
  71. ** Note: You may only render within the rectangle
  72. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  73. */
  74.  
  75. -> Note2: The following 'obj' isn't really a 'PTR TO mydata',
  76. ->        but obj must be a 'PTR TO <someobject>' to use the
  77. ->        macros like _mleft() etc. (see comments in mui.e)
  78. ->        I hope Wouter will change this in a futur version
  79. ->        of E !
  80.  
  81. PROC mDraw(cl:PTR TO IClass,obj:PTR TO mydata,msg:PTR TO MUIP_Draw)
  82.  
  83.     DEF i
  84.  
  85.     /*
  86.     ** let our superclass draw itself first, area class would
  87.     ** e.g. draw the frame and clear the whole region. What
  88.     ** it does exactly depends on msg.flags.
  89.     */
  90.  
  91.     DoSuperMethodA(cl,obj,msg)
  92.  
  93.     /*
  94.     ** if MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  95.     ** MUI just wanted to update the frame or something like that.
  96.     */
  97.  
  98.     IFN msg.flags & MADF_DRAWOBJECT THEN RETURN
  99.  
  100.     /*
  101.     ** ok, everything ready to render...
  102.     */
  103.  
  104.     SetAPen(_rp(obj),_dri(obj).Pens[TEXTPEN])
  105.  
  106.     FOR i:=_mleft(obj) TO _mright(obj) STEP 5
  107.         Move(_rp(obj),_mleft(obj),_mbottom(obj))
  108.         Draw(_rp(obj),i,_mtop(obj))
  109.         Move(_rp(obj),_mright(obj),_mbottom(obj))
  110.         Draw(_rp(obj),i,_mtop(obj))
  111.     ENDFOR
  112.  
  113. ENDPROC
  114.  
  115.  
  116. /*
  117. ** Here comes the dispatcher for our custom class. We only need to
  118. ** care about MUIM_AskMinMax and MUIM_Draw in this simple case.
  119. ** Unknown/unused methods are passed to the superclass immediately.
  120. */
  121.  
  122. PROC MyDispatcher(cl:PTR TO IClass IN a0,obj IN a2,msg:PTR TO Msg IN a1)(LONG)
  123.  
  124.     SELECT msg.MethodID
  125.     CASE MUIM_AskMinMax; RETURN AskMinMax(cl,obj,msg)
  126.     CASE MUIM_Draw     ; RETURN mDraw    (cl,obj,msg)
  127.     ENDSELECT
  128.  
  129.     RETURN DoSuperMethodA(cl,obj,msg)
  130. ENDPROC
  131.  
  132.  
  133.  
  134. /***************************************************************************/
  135. /* Thats all there is about it. Now lets see how things are used...        */
  136. /***************************************************************************/
  137. DEF    MUIMasterBase
  138.  
  139. PROC main()
  140.  
  141.     DEF    app=NIL,window,myobj,sigs=0,
  142.             mcc=NIL:PTR TO MUI_CustomClass
  143.  
  144.     IFN MUIMasterBase:=OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN) THEN
  145.         Raise('Failed to open muimaster.library')
  146.  
  147.     /* Create the new custom class with a call to MUI_CreateCustomClass().*/
  148.  
  149.     IFN mcc:=MUI_CreateCustomClass(NIL,MUIC_Area,NIL,SIZEOF_mydata,&MyDispatcher) THEN
  150.         Raise('Could not create custom class.')
  151.  
  152.     app:=ApplicationObject,
  153.         MUIA_Application_Title      , 'Class1',
  154.         MUIA_Application_Version    , '$VER: Class1 12.9E (26.11.95)',
  155.         MUIA_Application_Copyright  , '©1993, Stefan Stuntz',
  156.         MUIA_Application_Author     , 'Stefan Stuntz & JHS',
  157.         MUIA_Application_Description, 'Demonstrate the use of custom classes.',
  158.         MUIA_Application_Base       , 'CLASS1',
  159.  
  160.         SubWindow, window := WindowObject,
  161.             MUIA_Window_Title, 'A Simple Custom Class',
  162.             MUIA_Window_ID   , "CLS1",
  163.             WindowContents, VGroup,
  164.  
  165.                 Child, myobj := NewObject(mcc.Class,NIL,
  166.                     TextFrame,
  167.                     MUIA_Background, MUII_BACKGROUND,
  168.                     End,
  169.                 End,
  170.             End,
  171.         End
  172.  
  173.     IFN app THEN Raise('Failed to create Application.')
  174.  
  175.     DoMethodA(window,[MUIM_Notify,MUIA_Window_CloseRequest,MUI_TRUE,
  176.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit])
  177.  
  178.  
  179. /*
  180. ** This is the ideal input loop for an object oriented MUI application.
  181. ** Everything is encapsulated in classes, no return ids need to be used,
  182. ** we just check if the program shall terminate.
  183. ** Note that MUIM_Application_NewInput expects sigs to contain the result
  184. ** from Wait() (or 0). This makes the input loop significantly faster.
  185. */
  186.  
  187.     set(window,MUIA_Window_Open,MUI_TRUE)
  188.  
  189.     WHILEN DoMethodA(app,[MUIM_Application_NewInput,&sigs])=MUIV_Application_ReturnID_Quit
  190.         IF sigs THEN sigs := Wait(sigs)
  191.     ENDWHILE
  192.  
  193.     set(window,MUIA_Window_Open,FALSE)
  194.  
  195. /*
  196. ** Shut down...
  197. */
  198.  
  199. EXCEPTDO
  200.     IF app THEN MUI_DisposeObject(app)                /* dispose all objects. */
  201.     IF mcc THEN MUI_DeleteCustomClass(mcc)            /* delete the custom class. */
  202.     IF MUIMasterBase THEN CloseLibrary(MUIMasterBase) /* close library */
  203.     IF exception THEN PrintF('\s\n',exception)
  204. ENDPROC
  205.